<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;

/**
 * 服务信息
 */
class Worker extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['getbang'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * @return 提交信息申请
     */
    public function submits()
    {
        $user=$this->auth->getUser();

        $data = $this->request->post();

        $info=Db::name('worker')->where('user_id',$user['id'])->find();

        if ($info){
            $this->error(__('您已提交过申请'));
        }

        if (!$data['name']||!$data['mobile']||!$data['citys']){
            $this->error(__('请填写信息'));
        }



        $data['user_id']=$user['id'];

        $data['addtime']=time();

        $res=Db::name('worker')->insert($data);

        $this->success('提交成功');

    }

    /**
     * 陪诊人信息
     */
    public function getinfo(){

        $user=$this->auth->getUser();

        $info=Db::name('worker')->where('user_id',$user['id'])->find();

        $this->success('信息',$info);


    }


    /**
     * 修改信息
     */
    public function setinfo()
    {
        $user=$this->auth->getUser();

        $data['avatar'] = $this->request->post('avatar');
        $data['name'] = $this->request->post('name');
        $data['mobile'] = $this->request->post('mobile');
        $data['hsp'] = $this->request->post('hsp');


        Db::name('worker')->where('user_id',$user['id'])->update($data);

        $this->success('修改成功');

    }


    /*
     *  我的订单
     * */
    public function getmyorder()
    {
        $user=$this->auth->getUser();

        $worker=Db::name('worker')->where('user_id',$user['id'])->find();

        $status=$this->request->param('status');

        $map['wid']=['eq',$worker['id']];


        if ($status<1){
            $map['status'] = [['gt', 0], ['lt', 3]];
        }else{
            $map['status']=['eq',$status];
        }

        $list=Db::name('order')
            ->where($map)
            ->select();

        foreach ($list as $k=>$val){

            $list[$k]['addtime']=date('Y-m-d H:i:s',$val['addtime']);

            $hospital=Db::name('hospital')->where('id',$val['hid'])->find();
            $list[$k]['hpname']=$hospital['name'];
            $list[$k]['hpimage']=cdnurl($hospital['image'],true);



            $server=Db::name('hospital_service')->where('id',$val['sid'])->find();

            $list[$k]['sname']=$server['name'];
            $list[$k]['simage']=cdnurl($server['image'],true);


            $down=Db::name('user_down')->where('id',$val['did'])->find();

            $list[$k]['dname']=$down['name'];


        }


        $this->success('成功1',$list);

    }


    /**
     * 订单完成
     */
    public function ordersuccess()
    {
        $id = $this->request->param('id');

        $user=$this->auth->getUser();

        Db::name('order')
            ->where('id',$id)
            ->update(['status'=>3]);

        $order=Db::name('order')->where('id',$id)->find();

        // 添加金额
        moneyadd($user['id'],$order['money'],'陪诊人完成订单,获得金额');

        $this->success('成功');

    }


    /**
     * 订单完成
     */
    public function suoding()
    {
        $id = $this->request->param('id');

        $user=$this->auth->getUser();

        Db::name('order')
            ->where('id',$id)
            ->update(['status'=>2]);


        $this->success('成功');

    }
    /**
     * 排班信息
     */

    /**
     * 排班信息
     */

    public function getbang(){
        $user=$this->auth->getUser();

        // 获取未来7天的日期和星期
        $futureDays = [];
        $weekMap = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];

        for ($i = 1; $i <= 7; $i++) {
            $timestamp = strtotime("+$i days");
            $date = date('m-d', $timestamp);
            $week = $weekMap[date('w', $timestamp)];

            $futureDays[] = [
                'date' => $date,
                'week' => $week,
                'timestamp' => $timestamp
            ];


            $bang=Db::name('worker_bang')
                ->where('uid',$user['id'])
                ->where('date',$date)->find();

            if (!$bang){
                Db::name('worker_bang')->insert([
                    'uid'=>$user['id'],
                    'addt'=>'',
                    'endt'=>'',
                    'status'=>0,
                    'date'=>$date,
                    'week'=>$week,
                    'timestamp'=>$timestamp
                ]);

            }
        }

        //明天
        $mt=strtotime(date('y-m-d',strtotime('+1 day')));


        $lists=Db::name('worker_bang')
            ->where('uid',$user['id'])
            ->where('timestamp','gt',$mt)
            ->order('timestamp asc')
            ->select();




        $this->success('排班信息', $lists);
    }

    /**
     * 修改信息
     *  id,addt:addt,endt:endt,status:
     */
    public function setbang(){

        $data['id'] = $this->request->post('id');
        $data['addt'] = $this->request->post('addt');
        $data['endt'] = $this->request->post('endt');
        $data['status'] = $this->request->post('status');

        $data['status']=$data['status'];
        Db::name('worker_bang')->update($data);


        $this->success('修改成功');

    }















}